package demo2;

import java.io.*;
import java.net.*;

public class MyClient1 {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("127.0.0.1",1680);
            PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));

            System.out.println("已连接到图形面积计算器服务器，请输入所求面积的图形");
            System.out.println("输入quit断开连接");
            while (true) {
                System.out.println("1.rectangle/2.triangle/3.circle");
                String s = sin.readLine();
                String s1;

                if("rectangle".equalsIgnoreCase(s)){
                    System.out.println("请输入长和宽");
                    s1 = sin.readLine();
                } else if ("triangle".equalsIgnoreCase(s)) {
                    System.out.println("请输入三边长");
                    s1 = sin.readLine();
                } else if ("circle".equalsIgnoreCase(s)) {
                    System.out.println("请输入半径");
                    s1 = sin.readLine();
                } else if ("quit".equalsIgnoreCase(s)) {
                    System.out.println("连接正在关闭... ...");
                    break;
                }else{
                    System.out.println("输入错误，请重新输入\n");
                    continue;
                }

                s = s + " " + s1;
                out.println(s);

                // 等待服务器响应
                String serverResponse = in.readLine();
                if (serverResponse != null) {
                    System.out.println("@ 服务器计算面积为: " + serverResponse);

                    // 检查服务器是否要结束通信
                    if ("bye".equalsIgnoreCase(serverResponse)) {
                        System.out.println("服务器已关闭连接");
                        break;
                    }else {
                        System.out.print("请输入选择");
                    }
                }
            }
            out.close();
            in.close();
            socket.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
